package com.topone.service.impl;

import com.topone.dataObject.Bussiness;
import com.topone.dataObject.Mapper.BussinessMapper;
import com.topone.dataObject.ShopApply;
import com.topone.dto.BussinessDTO;
import com.topone.repository.BussinessRepository;
import com.topone.repository.ShopApplyRepository;
import com.topone.service.BussinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/6,0006.
 */
@Service
@Slf4j
public class  BussinessServiceImpl implements BussinessService{

    @Autowired
    private BussinessRepository repository;

    @Autowired
    private ShopApplyRepository shopApplyRepository;


    @Autowired
    private BussinessMapper bussinessMapper;

    //根据商家名称模糊查询
    @Override
    public List<BussinessDTO> findBussinessNameList(String bussinessName) {

            return bussinessMapper.findBussinessByBussinessNameLike(bussinessName);

    }

    //根据id查询商家信息
    @Override
    public Bussiness findOne(String bussinessCode,String bussinessId) {
        Bussiness bussiness =new Bussiness();

            bussiness = repository.findByBussinessCodeAndBussinessId(bussinessCode,bussinessId);

        return bussiness;
    }

    //根据商家名称查询
    @Override
    public Bussiness findByBussinessName(String bussinessName,Integer status) {
       Bussiness bussiness =new Bussiness();
        if(bussinessName!=null && !bussinessName.isEmpty()){
            bussiness=bussinessMapper.bussiness(bussinessName,status);
        }
        return bussiness;
    }

    //查询所有的商家信息
    @Override
    public Page<Bussiness> findAll(Pageable pageable) {
        Page<Bussiness> bussinessList=repository.findAll(pageable);
        return bussinessList;
    }

    //修改或新增商家信息
    @Override
    @Transactional
    public Bussiness save(Bussiness bussiness, ShopApply shopApply) {
        if (shopApply != null) {
            if (!StringUtils.isEmpty(shopApply.getShopId())){
                shopApplyRepository.save(shopApply);
            }
        }
        return repository.save(bussiness);
    }

    //查询一个商家所有的店铺信息
    @Override
    public Page<Bussiness> findList(String bussinessCode,Pageable pageable) {
        return repository.findByBussinessCode(bussinessCode,pageable);
    }

    //查询所有已入驻商家
    @Override
    public Page<Bussiness> findUpAll(Integer bussinessStatus, Pageable pageable) {
        return repository.findByBussinessStatus(bussinessStatus,pageable);
    }

    //查询所有有效商家信息
    @Override
    public List<BussinessDTO> allBussiness(Integer status) {
        return bussinessMapper.allBussiness(status) ;
    }

    @Override
    public BussinessDTO checkBussinessPassword(String bussinessPassword, String bussinessPhone) {
        return bussinessMapper.findBussinessId(bussinessPassword,bussinessPhone);
    }
}